package com.big.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.big.app.dao.UserDao;
import com.big.app.dao.UserLogDao;
import com.big.app.entity.User;
import com.big.app.entity.UserLog;
import com.big.app.service.UserService;
import com.big.app.util.JedisUtils;
import com.big.app.util.PassUtils;
import com.big.app.util.StringUtils;
import com.big.app.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao dao;
    @Autowired
    private UserLogDao logDao;
    @Value("${user.passkey}")
    private String key;

    @Override
    public R login(String phone, int code, String ip) {
        if (StringUtils.isNoyEmpty(phone)&& code >0) {
            if (phone.matches("^1[3-9]{1}[0-9]{9}$")) {
                /*查询是否存在验证码*/
                if(JedisUtils.esists("user_code_login_"+phone)){
                    /*存在，判断验证码的正确*/
                    if(code==Integer.parseInt(JedisUtils.getStr("user_code_login_"+phone))){
                        /*验证码正确   比对手机号是否注册过 ,不存在 自动注册*/
                        /*查询用户*/
                        User user = dao.selectOne(new QueryWrapper<User>().eq("phone", phone));
                        if (user == null) {
                            user=new User();
                            user.setFlag(1);
                            user.setCtime(new Date());
                            user.setPhone(phone);
                            /*写入日志*/
                            logDao.insert(new UserLog(user.getId(), 1, "手机号验证码登陆", ip, new Date()));
                        }
                        /*唯一登陆验证*/
                        if(JedisUtils.esists("user_login_phone"+phone)){
                            //记录被迫挤掉登陆
                            JedisUtils.setSet("login_lose",JedisUtils.getStr("user_login_phone"+phone));
                            /*清理原来令牌*/
                            JedisUtils.del("login_token"+JedisUtils.getStr("user_login_phone"+phone));
                        }

                        /*生成令牌  ase算法*/
                        String token = PassUtils.aesenc(key, phone + "_" + System.currentTimeMillis());
                        /*记录本次令牌  和用户的数据*/
                        JedisUtils.setStr("login_token"+token, JSON.toJSONString(user),1800);
                        //记录手机号对应的，令牌 目的：验证唯一登录
                        JedisUtils.setStr("user_login_phone"+phone, token,1800);
                        /*删除手机号验证码*/
                        JedisUtils.del("user_code_login_"+phone);
                        //登陆日志
                        logDao.insert(new UserLog(user.getId(), 1, "手机号验证码登陆", ip, new Date()));
                        return new R(200,"ok",user);
                    }
                }
            }
        }
                return new R(400,"false","网络有点慢，请尝试");
    }

    @Override
    public R query(String token) {
        /*通过令牌查询用户*/
        if (StringUtils.isNoyEmpty(token)&&token.length() > 0) {
            /*验证令牌是否存在有效*/
            if(JedisUtils.esists("login_token"+token)){
                //查询用户
                User user = JSON.parseObject(JedisUtils.getStr("login_token" + token), User.class);
                return  new R(200 ,"ok",user);

            }
        }
        return new R(400,"false","令牌无效");
    }

    @Override
    public R checkToken(String token) {
        //校验令牌是否有效
        if(StringUtils.isNoyEmpty(token)){
            /*检查当前令牌是否挤掉过*/
            if(JedisUtils.check("login_lose",token)){
                return new R(400,"false","手机号在别处登陆过");
            }else{
                //验证令牌是否有效
                if(JedisUtils.esists("login_token"+token)){
                    return new R(200,"ok","令牌有效");
                }
            }
        }
       return new R(400,"false","令牌无效");
    }

    @Override
    public R loginout(String token) {
        /*退出登陆*/
        if(StringUtils.isNoyEmpty(token)){
            if(JedisUtils.esists("login_token"+token)){
                User user = JSON.parseObject(JedisUtils.getStr("login_token" + token), User.class);
               //删除redis中的手机号和令牌
               JedisUtils.del("login_token"+token,"user_login_phone"+user.getPhone());
                return  new R(200 ,"ok",user+"退出登陆");

            }
        }
        return  new R(400 ,"false","无效令牌");
    }
}
